<!--
 * @Descripttion: 
 * @version: 
 * @Author: mmzhang
 * @Date: 2021-07-21 10:45:02
 * @LastEditTime: 2022-12-19 13:47:15
-->
<template>
  <div class="group-report">
    <dt-search ref="search" :labelWidth="!showFlag ? 85 : 100" :xl="5" :xlBtn="2"></dt-search>
    <div class="btn-content">
      <Button type="primary" @click="adjustHeader()">调整表头排序</Button>
      <Poptip v-model="exportVisible" trigger="hover" transfer placement="bottom">
        <Button type="primary" style="margin-left: 8px" v-if="unlocking('0207020101')">导出</Button>
        <div slot="content">
          <div class="export-btn hand" @click="exportExcelFun(0)">全部导出</div>
          <div class="export-btn hand" @click="exportExcelFun(1)">按事业部导出</div>
          <div class="export-btn hand" @click="exportExcelFun(2)">按公司导出</div>
          <div class="export-btn hand" @click="exportExcelFun(3)">按事业处导出</div>
          <div class="export-btn hand" @click="exportExcelFun(4)">按案场导出</div>
          <div class="export-btn hand" @click="exportExcelFun(5)">按产品导出</div>
        </div>
      </Poptip>
      <Button
        type="primary"
        class="operation"
        style="margin-left: 8px"
        v-if="unlocking('0207020102')"
        @click="operationClick">
        查看导出记录
      </Button>
    </div>
    <div style="text-align: center; cursor: pointer" @click="showSearchFun">
      <p v-if="!showFlag">
        查看更多筛选
        <Icon type="ios-arrow-down" />
      </p>
      <p v-else>
        收起筛选
        <Icon type="ios-arrow-up" />
      </p>
    </div>
    <dt-grid
      ref="table"
      :columns="columns"
      :onFetch="onFetch"
      :tree="{
        hasChild: 'childrenSize',
        loadMethod: (row, data) => onFetch({ ...data, orgId: row.orgCompanyId })
      }"
      style="margin-top: 15px"></dt-grid>
    <!--表头排序-->
    <dynamic-header
      ref="dynamicHeader"
      @fieldCancel="fieldCancel"
      @headUpdate="headUpdate"
      :sourcedata="sourcedata"
      :targetdata="targetdata"></dynamic-header>
    <operation-record ref="operationRecord"></operation-record>
  </div>
</template>
<script>
  import columnConfig from './components/groupColumn'
  import groupMgr from '@/biz/businessReport/group'
  import businessCommonMgr from '@/biz/businessReport/businessCommon'
  import tool from '@/common/tool.js'
  import dt from '@dt/dt'
  import watermark from '@/common/waterMark'
  import dynamicHeader from '@/components/dynamicHeader.vue'
  import groupSearch from './components/groupSearch'
  export default {
    data() {
      return {
        exportVisible: false,
        formValidate: {
          companyIds: [],
          checkTime: tool.currentMonth(),
          coopModes: [], // 合作模式
          developerAbbreviations: [], //开发商简称
          productTypes: [], // 产品类型
          quantityTypes: ['1', '2', '3', '4'], // 套数取值类型默认不选中“车位、车库、储藏室、地下室”
          areaTypes: [], // 面积
          amountTypes: [], // 金额
          buildingName: '' //案场名称
        },
        tableData: [],
        columns: [],
        filterParams: {},
        sourcedata: [],
        targetdata: [],
        showFlag: false
      }
    },
    watch: {
      $route(to, from) {
        if (to.name != 'groupReport') {
          return false
        }
        if (from.name != 'contractSummary' && from.name != 'subSummary') {
          this.$refs.search.reset()
          watermark.set()
          this.getHeadList()
        }
      }
    },
    components: {
      dynamicHeader
    },
    created() {
      this.getHeadList()
    },
    mounted() {
      //发送this 到 js 文件里
      columnConfig.sendThis(this)
      this.$refs.table.init(this)
      this.$refs.search.init(this.$refs.table, new groupSearch(this).options(), this.formValidate)
      this.$refs.table.fetch()
      watermark.set()
    },
    methods: {
      operationClick() {
        this.$refs.operationRecord.show('export_groupReport', 'M_7', '', 'logtype')
      },
      showSearchFun() {
        this.showFlag = !this.showFlag
        this.$refs.search.update(new groupSearch(this).options(), this.formValidate)
      },
      adjustHeader() {
        this.$refs.dynamicHeader.show()
      },
      fieldCancel() {
        this.$refs.dynamicHeader.hide()
      },
      headUpdate(params) {
        businessCommonMgr
          .reportHeadUpdate({
            headType: 1,
            updateHead: params.updateInfo
          })
          .then((res) => {
            this.getHeadList()
            this.$refs.dynamicHeader.hide()
          })
          .catch((err) => {})
      },
      getHeadList() {
        businessCommonMgr
          .reportHeadList({
            headType: 1
          })
          .then((res) => {
            this.sourcedata = res.hideHead
            this.targetdata = res.showHead
            this.targetdata.forEach((item, index) => {
              item.disabled = item.fixed
            })
            this.columns = []
            let baseColumns = columnConfig.columns
            this.targetdata.forEach((item) => {
              let column = baseColumns.filter(
                (baseItem) => item.fieldKey == baseItem.field.split('_')[baseItem.field.split('_').length - 1]
              )
              if (column.length) {
                column[0].field = item.fieldKey
                this.columns.push(column[0])
              }
            })
          })
          .catch((err) => {})
      },
      // 导出
      exportExcelFun(type) {
        if (!this.tableData.length) {
          this.$Message.warning('暂无数据！')
          return false
        }
        let params = Object.assign(this.filterParams)

        params.exportType = type

        groupMgr.exportGroupReport(params)
      },
      onFetch(data) {
        let params = Object.assign(data)

        params.startTime = params.checkTime[0]
        params.endTime = params.checkTime[1]
        params.quantityTypes = params.quantityTypes.length ? params.quantityTypes : params.productTypes
        params.areaTypes = params.areaTypes.length ? params.areaTypes : params.productTypes
        params.amountTypes = params.amountTypes.length ? params.amountTypes : params.productTypes
        this.filterParams = { ...params }

        return groupMgr.getGroupReportList(params).then((res) => {
          if (params.orgId) {
            res = res[0].children[0].children
          } else {
            if (params.buildingName === '' || params.buildingName === null) {
              res.forEach((item) => {
                this.$refs.table.treeExpand(item)
              })
            } else {
              this.resolveExpand(res)
            }
            this.tableData = res
          }
          return res
        })
      },
      // 处理列表展开
      resolveExpand(arr) {
        arr.forEach((item) => {
          this.$refs.table.treeExpand(item)
          if (item.children && item.children.length) {
            this.resolveExpand(item.children)
          }
        })
      },
      // 跳转定单汇总 type 0 认购单 1签约单  category 0 套数 1面积 2 金额,statusType 0认购 1退购,2签约 3 退签
      linkOrderSummaryFun(rowParams) {
        let type = rowParams[0]
        let category = rowParams[1]
        let row = rowParams[2]
        let statusType = rowParams[3]

        let containDate = []
        // statusType 0认购 1退购,2签约 3 退签 4全款
        //containDate 1 认购 2退购,5签约 3 退签 6 全款
        if (statusType == 0) {
          containDate = ['1']
        } else if (statusType == 1) {
          containDate = ['2']
        } else if (statusType == 2) {
          containDate = ['5']
        } else if (statusType == 3) {
          containDate = ['3']
        } else if (statusType == 4) {
          containDate = ['6']
        } else {
          containDate = []
        }

        // 注意区分四级产品的点击
        let params = {
          orgId: row.productId != '' ? row.housesId : row.orgEndId,
          checkTime: this.filterParams.checkTime,
          productId: row.productId,
          productTypes: !category
            ? this.filterParams.quantityTypes
            : category == 1
            ? this.filterParams.areaTypes
            : this.filterParams.amountTypes,
          coopModes: this.filterParams.coopModes, // 合作模式
          developerAbbreviations: this.filterParams.developerAbbreviations, //开发商简称
          sourceChannels: [], // 来源渠道
          status:
            statusType == 0
              ? ['4', '6', '7', '8']
              : statusType == 1
              ? ['6']
              : statusType == 2
              ? ['10104', '10106', '10109']
              : ['10106'],
          isBackDate: statusType == 1 || statusType == 3,
          containDate: containDate,
          checkIndex: null
        }
        type ? dt.memory.set('conSumOrder_params', params) : dt.memory.set('subSumOrder_params', params)
        this.$router.push({
          name: type ? 'contractSummary' : 'subSummary',
          params: {
            showNetWorthFlag: true
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .group-report {
    padding: 20px;
    width: 100%;

    .ivu-spin-fix {
      background-color: rgba(255, 255, 255, 0.35);
      width: calc(100% - 200px);
      left: 200px;
    }

    .dt-search {
      ::v-deep .ivu-form {
        .ivu-col:nth-child(2) {
          .ivu-form-item-content {
            margin-left: 0 !important;
          }
        }

        .ivu-col-span-xl-3 {
          .ivu-form-item-content {
            margin-left: 0 !important;
          }
        }

        .ivu-form-item-label {
          padding: 10px 5px 10px 0;
        }

        .ivu-form-item {
          margin-bottom: 20px;
        }

        .ivu-btn {
          padding: 0 3px;
          margin-right: 5px;
        }

        .ivu-select-multiple .ivu-tag {
          max-width: 55%;
          padding: 0;
          margin: 3px 0;
        }
      }
    }

    .ivu-btn-primary {
      padding: 0 3px;
    }
  }

  .export-btn {
    line-height: 30px;
    cursor: pointer;
    color: #2d8cf0;
    text-align: center;
    border-bottom: 1px #e8eaec solid;
  }

  .btn-content {
    position: absolute;
    right: 25px;

    .ivu-btn-primary {
      padding: 0 3px;
    }
  }
</style>
